package com.aistar.service.employer.impl;

import com.aistar.mapper.*;
import com.aistar.model.dto.employer.BlacklistWithEmployeeNameDTO;
import com.aistar.model.dto.employer.EmployerAndUserDTO;
import com.aistar.model.entity.Company;
import com.aistar.model.entity.Employer;
import com.aistar.model.entity.EmployerBlacklist;
import com.aistar.model.entity.User;
import com.aistar.service.employer.IEmployerCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: EmployerCenterServiceImpl
 * @author: LiuWenXiu
 * @date: 2021/9/28 20:15
 * @Description: TODO...本类用来演示:
 */
@Service
@Transactional
public class EmployerCenterServiceImpl implements IEmployerCenterService {

    @Autowired
    private EmployerMapper employerMapper;

    @Autowired
    private EmployerBlacklistMapper employerBlacklistMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ResumeMapper resumeMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    // 根据招聘者id加载招聘者个人信息(头像,姓名,电话)
    @Override
    public EmployerAndUserDTO loadMyInfo(Integer id) {
        EmployerAndUserDTO employerAndUserDTO = new EmployerAndUserDTO();
        // 获取该招聘者id对应的招聘者实体
        Employer employer = employerMapper.findById(id);
        employerAndUserDTO.setEmployer(employer);
        // 根据该招聘者id获取用户实体
        User user = userMapper.findById(employer.getUserId());
        employerAndUserDTO.setUser(user);
        return employerAndUserDTO;
    }

    // 根据招聘者id加载招聘者公司信息
    @Override
    public Company loadCompanyInfo(Integer id) {
        Company company = companyMapper.partCompany(id);
        if (null == company)
            return null;
        return company;
    }


    @Override
    public List<BlacklistWithEmployeeNameDTO> loadBlacklist(Integer id) {
        List<BlacklistWithEmployeeNameDTO> lists = new ArrayList<>();
        // 首先根据招聘者id加载黑名单的内容
        List<EmployerBlacklist> employerBlacklists = employerBlacklistMapper.findByEmployerId(id);
        // 判断该招聘者是黑名单是否有内容
        if (null == employerBlacklists || employerBlacklists.size() == 0) {
            return null;// 直接返回空值
        }
        // 到这说明该招聘者的黑名单里是有东西的,遍历集合
        // 根据黑名单的id加载求职者的name
        Integer count = 1;
        for (EmployerBlacklist employerBlacklist : employerBlacklists) {
            String name = employeeMapper.findNameById(employerBlacklist.getEmployeeId());
            // 把这些数据插入到第三方实体类中
            BlacklistWithEmployeeNameDTO res = new BlacklistWithEmployeeNameDTO(count++, employerBlacklist.getId(), employerBlacklist.getEmployeeId(), employerBlacklist.getEmployerId(), name);
            lists.add(res);
        }
        return lists;
    }

    @Override
    public int removeBlacklist(Integer id) {
//        根据黑名单id删除黑名单中的数据,同时需要删除redis中的数据
        // redis中存储的数据,key是公司id,value是求职者id[集合]

        // 首先根据黑名单id获取招聘者的id
        Integer employerId = employerBlacklistMapper.findById(id).getEmployerId();
        // 获取求职者id
        Integer employeeId = employerBlacklistMapper.findById(id).getEmployeeId();
        // 根据招聘者id获取公司id
        Integer companyId = employerMapper.findCompanyIdById(employerId);
        // 移除redis中的数据
        SetOperations sot = redisTemplate.opsForSet();
        sot.remove("erb_"+companyId,employeeId.toString());
//        System.out.println("正在删除erb_"+companyId+"value为:"+employeeId+"的redis数据");
        // 删除数据库的记录
        return employerBlacklistMapper.delById(id);
    }

    @Override
    public int pullBlack(Integer resumeId, Integer employerId) {
        // 首先需要根据简历id查看求职者id
        Integer employeeId = resumeMapper.findEmployeeIdById(resumeId);
        // 获取招聘者公司id
        Integer companyId = employerMapper.findCompanyIdById(employerId);
        // 获取redis
        SetOperations sop = redisTemplate.opsForSet();
        String key = "erb_"+companyId;
        String value = employeeId.toString();
        // 判断该用户的黑名单是否有该求职者 - 但是逻辑上来说如果招聘者屏蔽了求职者,求职者是不允许该招聘者发送简历的
        // 保险起见还是判断一下
        // 首先在redis中进行判断
        // 判断有没有该公司和求职者
        if(sop.isMember(key,value)){
            // 如果有,表示已经屏蔽了该用户,直接返回-1
            System.out.println("redis中有数据,直接返回false");
            return -1;
        }
        // 如果没有则到数据库中进行判断
        // 获取该招聘者的屏蔽列表
        List<EmployerBlacklist> employerBlacklists = employerBlacklistMapper.findByEmployerId(employerId);
        // 首先判断是否为空
        if (null != employerBlacklists && employerBlacklists.size() > 0) {
            // 如果不为空则需要进行遍历判断是否有该人
            for (EmployerBlacklist employerBlacklist : employerBlacklists) {
                if (employerBlacklist.getEmployeeId() == employeeId) {
                    // 说明有,则添加失败
                    // 查询出有的结果后还需要存储到redis中
                    sop.add(key,value);
                    redisTemplate.expire(key,7, TimeUnit.DAYS);
                    return -1;  // 返回-1表示屏蔽列表已经有他了
                }
            }
        }
        // 如果没有,则可以进行添加
        // 先把数据添加到redis中并设置过期时间为7天
        sop.add(key,value);
        redisTemplate.expire(key,7, TimeUnit.DAYS);
        int count = employerBlacklistMapper.insertBlacklist(employerId, employeeId);
        return count;
    }
}
